<template>
  <div class="app-container">
    <el-form @submit.native.prevent :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px" :label-position="labelPosition">
      <el-form-item label="客户" prop="khmc">
        <el-input
          v-model="queryParams.khmc"
          :disabled="true"
        />
      </el-form-item>
    </el-form> 
    <el-table
      v-loading="loading"
      :data="customerCategoryDaysList"
    >
      <af-table-column prop="ssrqid" label="日期" fixed="left"></af-table-column>
      <af-table-column prop="spjsi" label="入仓件数"></af-table-column>
      <af-table-column prop="spsli" label="入仓重量(吨)"></af-table-column>
      <af-table-column prop="spjso" label="出仓件数"></af-table-column>
      <af-table-column prop="spslo" label="出仓重量(吨)"></af-table-column>
      <af-table-column prop="spjs" label="结余件数"></af-table-column>
      <af-table-column prop="spsl" label="结余重量(吨)"></af-table-column>
    </el-table>
     <pagination
        v-show="total>0"
        :total="total"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        @pagination="getList"
      />
  </div>
</template>

<script>
import { getListCustomerCategoryDays } from "@/api/storage/dataAnalysis/customerCategory";
import { NUMBER_DIDGIT, WEIGHT_DIGIT } from '@/utils/constant'

export default {
  name: "CustomerCategoryDays",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      //对齐
      labelPosition: "right",
      //加载列表
      loading: true,
      // 查询参数
      queryParams: {
        khmc:"",
        ally:"" ,
        khid:"" ,
        khlx:"" ,
        khmcid:"" ,
        rqid:"" ,
        spckid:"" ,
        spkhmcid:"" ,
        ssrqid:"" ,
        ssrqid1:"" ,
        ssrqid2:"" ,
        zt: ""
      },
      // 按日区分列表
      customerCategoryDaysList: [],
      //页数
      pageSize: 10,
      //页码
      pageNum:1,
      // 总条数
      total: 0,
    };
  },
  beforeRouteEnter(to, from, next) {
    const query = to.query
    next(vm => {   
      if(Object.keys(query).length > 0) {
        vm.queryParams.spkhmcid = query.khid
        vm.queryParams.khmc = query.khmc
        vm.getList()
      }
    })
  },
  methods: {
    /** 查询按日区分列表 */
    getList() {
      this.loading=true
      getListCustomerCategoryDays(this.queryParams, {pageNum:this.pageNum, pageSize:this.pageSize}).then(response => {
        this.customerCategoryDaysList = response.rows;
        this.total = response.total;
        this.loading=false
      });
    },

    //合计
    getSummaries({ columns, data }) {
      const sums = []
      columns.forEach((column, index) => {
        if(index === 0) {
          sums[index] = "合计"
			    return
        }
        if ((column.property === "spjs" || column.property === "spsl")) {
          const values = data.map((item) => {
            return parseFloat(this.delcommafy(item[column.property]))
          });
          
          sums[index] = values.reduce((prev, curr) => {
            if(column.property === "spsl") {
              return parseFloat((prev + curr).toFixed(WEIGHT_DIGIT))
            }
            if(column.property === "spjs") {
              return parseFloat((prev + curr).toFixed(NUMBER_DIDGIT))
            }
          }, 0);
        }
      })
      return sums
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.reset()
      this.getList()
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
    },
  }
};
</script>
